package com.mkvld.appietijdenv2.dialogs;

import com.mkvld.appietijdenv2.R;
import com.mkvld.appietijdenv2.activities.ActV9;
import com.mkvld.appietijdenv2.data.Shift;
import com.mkvld.appietijdenv2.databases.ShiftDatabase;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;

public class DialCopyMoveV9 extends DialogFragment {
	private boolean keepOriginal; // true for copy false for move
	protected Shift[] shifts;
	
	public static void showCopy(ActV9 activity, Shift[] shifts, boolean singleWeek) {
		showDialog(activity, shifts, singleWeek, true);
	}
	
	public static void showMove(ActV9 activity, Shift[] shifts, boolean singleWeek) {
		showDialog(activity, shifts, singleWeek, false);
	}
	
	private static void showDialog(ActV9 activity, Shift[] shifts, boolean singleWeek, boolean keepOriginal) {
		DialCopyMoveV9 dial = new DialCopyMoveV9();
		Bundle args = new Bundle();
		
		args.putSerializable("shifts", shifts);
		args.putBoolean("keepOriginal", keepOriginal);
		dial.setArguments(args);
		dial.show(activity.getSupportFragmentManager(), "");
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		keepOriginal = getArguments().getBoolean("keepOriginal");
		shifts = (Shift[]) getArguments().getSerializable("shifts");
	}
	
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		View v = getActivity().getLayoutInflater().inflate(R.layout.dial_copy_move, null, false);
		initView(v);
		int day = 0;
		if (shifts.length > 1)
			hideDayPicker();
		else
			day = shifts[0].getDay();
		
		setValues(shifts[0].getYear(), shifts[0].getWeek(), day);
		
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setView(v);
		if (keepOriginal)
			builder.setTitle(shifts.length > 1 ? R.string.dcm_copy_title : R.string.dcm_copy_single_title);
		else 
			builder.setTitle(shifts.length > 1 ? R.string.dcm_move_title : R.string.dcm_move_single_title);
		builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				for (Shift shift : shifts) {
					if (!keepOriginal)
						ShiftDatabase.db.deleteShift(shift);
					if (shifts.length == 1)
						shift.setDay(getDayValue());
					shift.setWeek(getWeekValue());
					shift.setYear(getYearValue());
					ShiftDatabase.db.addShift(shift);
				}
				((ActV9)getActivity()).update();
			}
		});
		
		builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		
		return builder.create();
	}
	
	protected void initView(View root) {
		
	}
	
	protected void hideDayPicker() {
		
	}
	
	protected int getDayValue() {
		return 0;
	}
	
	protected int getWeekValue() {
		return 0;
	}
	
	protected int getYearValue() {
		return 0;
	}
	
	protected void setValues(int year, int week, int day) {
		
	}
	
}
